﻿using System;
using System.Collections.Specialized;
using System.Windows.Controls;
using System.Windows.Data;
using ICommand = System.Windows.Input;
using Agnes.Core;
using DevExpress.Xpf.Bars;
using System.Collections.ObjectModel;
using DevExpress.Xpf.Docking;
using System.Collections.Generic;
using ICSharpCode.Core;

namespace Agnes.Mainframe
{
    /// <summary>
    /// MainframeView.xaml 的交互逻辑
    /// </summary>
    public partial class MainframeView : UserControl
    {
        private ICommand.ICommand _docCllosedCmd = new DocCloseCmd();

        public MainframeView()
        {
            try
            {
                InitializeComponent();

                foreach (BarItem i in AgnesServiceManager.AgnesWorkBenchService.BarItems)
                {
                    DxBarManager.Items.Add(i);
                }

                AgnesServiceManager.AgnesWorkBenchService.PropertyChanged
                    += (sender, args) =>
                    {
                        if (args.PropertyName == "FocusedRichEditControl")
                        {
                            if (AgnesServiceManager.AgnesWorkBenchService.FocusedRichEditControl != null)
                            {
                                AgnesServiceManager.AgnesWorkBenchService.FocusedRichEditControl.BarManager = DxBarManager;
                            }
                            else
                            {
                                RECDefaultRichEditControl.BarManager = DxBarManager;
                            }
                        }
                    };

                AgnesServiceManager.AgnesWorkBenchService.PropertyChanged
                    += (sender, args) =>
                           {
                               if (args.PropertyName == "FocusedDocumentPanel")
                               {
                                   foreach (BaseLayoutItem i in documentContainer.Items)
                                   {
                                       if (i.DataContext == AgnesServiceManager.AgnesWorkBenchService.FocusedDocumentPanel)
                                       {
                                           documentContainer.SelectedTabIndex = documentContainer.Items.IndexOf(i);
                                           break;
                                       }
                                   }
                               }
                           };


                foreach (IDocumentPanel i in AgnesServiceManager.AgnesWorkBenchService.DocumentPanels)
                {
                    AddDocumentPanel(i);
                }

                AgnesServiceManager.AgnesWorkBenchService.DocumentPanels.CollectionChanged
                    += (sender, args) =>
                           {
                               if (args.Action == NotifyCollectionChangedAction.Add)
                               {
                                   foreach (IDocumentPanel i in args.NewItems)
                                   {
                                       AddDocumentPanel(i);
                                   }
                               }
                               else if (args.Action == NotifyCollectionChangedAction.Remove)
                               {
                                   foreach (IDocumentPanel i in args.OldItems)
                                   {
                                       foreach (BaseLayoutItem j in documentContainer.Items)
                                       {
                                           if (j.DataContext == i)
                                           {
                                               documentContainer.Remove(j);
                                               break;
                                           }
                                       }
                                   }
                               }
                           };
            }
            catch (Exception ex)
            {
                AgnesServiceManager.LogServ.Error("Mainframe--初始化严重错误:" + ex.Message);
            }
            
        }

        void AddDocumentPanel(IDocumentPanel panel)
        {
            try
            {
                DocumentPanel documentPanel = new DocumentPanel();
                documentPanel.DataContext = panel;
                Binding binding = new Binding("Caption");
                documentPanel.SetBinding(DocumentPanel.CaptionProperty, binding);
                documentPanel.Content = panel.Content;
                documentPanel.CloseCommand = new DocCloseCmd();
                documentContainer.Items.Add(documentPanel);
                documentContainer.ClosingBehavior = ClosingBehavior.ImmediatelyRemove;
                documentPanel.GotFocus += (sender, args) =>
                                              {
                                                  AgnesServiceManager.AgnesWorkBenchService.FocusedDocumentPanel =
                                                     (sender as DocumentPanel).DataContext as IDocumentPanel;
                                              };
                documentPanel.LostFocus += (sender, args) =>
                                               {
                                                   AgnesServiceManager.AgnesWorkBenchService.FocusedDocumentPanel = null;
                                               };
                documentPanel.Focus();
            }
            catch (Exception ex)
            {
                AgnesServiceManager.LogServ.Error("Mainframe--Add Document Panel Error:" + ex.Message);
            }
        }

        private void NewSlnView_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            ContentControl contentControl = sender as ContentControl;
            contentControl.Content = (this.DataContext as MainframeViewModel).NewSlnView;
        }

        private void OpenSlnView_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            ContentControl contentControl = sender as ContentControl;
            contentControl.Content = (this.DataContext as MainframeViewModel).OpenSlnView;
        }

        private void AddIns_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            List<AddInInfo> AddInfos = new List<AddInInfo>();
            foreach(AddIn i in AddInTree.AddIns)
            {
                if (i.Properties["isapp"] == "true")
                {
                    AddInInfo a = new AddInInfo();
                    a.Name = i.Properties["name"];
                    a.Url = i.Properties["url"];
                    a.Author = i.Properties["author"];
                    a.CopyRight = i.Properties["copyright"];
                    a.Description = i.Properties["description"];
                    AddInfos.Add(a);
                }
            }
            lbAdds.ItemsSource = AddInfos;
        }
    }

    class AddInInfo
    {
        public string Name { get; set; }

        public string Url { get; set; }

        public string Author { get; set; }

        public string Description { get; set; }

        public string CopyRight { get; set; }
    }

    class DocCloseCmd : ICommand.ICommand
    {

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            AgnesServiceManager.AgnesWorkBenchService.DocumentPanels.Remove((parameter as DocumentPanel).DataContext as IDocumentPanel);
          /*  try
            {
                SaveDialog sd = new SaveDialog();
                sd.JustActiveDocument = true;
                sd.ShowSDialog();
                if (sd.SaveResult != SaveResult.Cancel)
                {
                   
                }
            }
            catch (Exception ex)
            {
                AgnesServiceManager.LogServ.Error("Mainframe--Document Close Error:" + ex.Message);
            }*/
        }
    }
}
